
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author enrico
 */
public class AggiungiUtenteView extends HBox {
  AnagraficaController controller;
  private boolean emptyNome=true,emptyCognome=true;
  private Button aggiungiButton;
  private GridPane grid = new GridPane();
  private TextField nomeField,cognomeField;
  private TextField indirizzoPrimoField = new TextField();
  private TextField indirizzoSecondoField = new TextField();
  private TextField telefonoField = new TextField();
  private TextField cellulareField = new TextField();
  private TextField noteField = new TextField();
  private TextField emailField = new TextField();
  
  public AggiungiUtenteView(AnagraficaController ac) {
    controller = ac;
    
    grid.setId("aggiungiGrid");
    
    ImageView personcina = new ImageView(new Image(getClass().getResourceAsStream("Stili/User.png")));
    nomeField = new TextField();
    cognomeField = new TextField();
    
    nomeField.setOnKeyReleased(new EventHandler<KeyEvent>() {

      @Override
      public void handle(KeyEvent arg0) {
        if(nomeField.getText().trim().isEmpty()) emptyNome=true;
        else emptyNome=false;
        
        checkPulsante();
      }
    });
    cognomeField.setOnKeyReleased(new EventHandler<KeyEvent>() {

      @Override
      public void handle(KeyEvent arg0) {
        if(cognomeField.getText().trim().isEmpty()) emptyCognome=true;
        else emptyCognome=false;
        
        checkPulsante();
      }
    });
    
    Text nome = new Text("Nome");
    Text cognome = new Text("Cognome");
    Text indirizzo = new Text("Indirizzo");
    Text telefono = new Text("Telefono");
    Text cellulare = new Text("Cellulare");
    Text note = new Text("Note");
    Text email = new Text("Email");

    grid.add(personcina, 0, 0, 2, 2);
    grid.add(nome, 1, 0);
    grid.add(nomeField, 2, 0);
    grid.add(cognome, 1, 1);
    grid.add(cognomeField, 2, 1);
    
    grid.add(indirizzo, 0, 3);
    grid.add(indirizzoPrimoField, 1, 3, 2, 1);
    grid.add(indirizzoSecondoField, 1, 4, 2, 1);
    grid.add(telefono, 0, 5);
    grid.add(telefonoField, 1, 5, 2, 1);
    grid.add(cellulare, 0, 6);
    grid.add(cellulareField, 1, 6, 2, 1);
    grid.add(email,0,7);
    grid.add(emailField,1,7,2,1);
    
    grid.add(note, 0, 8);
    grid.add(noteField, 1, 8, 2, 1);
    Insets porcodio = grid.getPadding();
    
    Button rimuoviButton = new Button("Annulla",new ImageView(new Image(getClass().getResourceAsStream("Stili/delete.png"))));
    rimuoviButton.setId("round-red");
    grid.add(rimuoviButton, 2, 9, 1, 1);
    rimuoviButton.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent arg0) {
        controller.releasePopup();
      }
    });
    
    aggiungiButton = new Button("Aggiungi",new ImageView(new Image(getClass().getResourceAsStream("Stili/plus.png"))));
    aggiungiButton.setId("green");
    aggiungiButton.setDisable(true);
    grid.add(aggiungiButton, 0, 9, 2, 1);
    aggiungiButton.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent arg0) {
        controller.aggiungiPersona(nomeField.getText(),cognomeField.getText(),indirizzoPrimoField.getText(),indirizzoSecondoField.getText(),telefonoField.getText(),cellulareField.getText(),noteField.getText(),emailField.getText());
      }
    });
    
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setAlignment(Pos.CENTER_RIGHT);
    
    this.getChildren().add(grid);
  }
  
  private void checkPulsante() {
    if (emptyCognome==false && emptyNome==false) 
      aggiungiButton.setDisable(false);
    else
      aggiungiButton.setDisable(true);
  }
}
